package com.jp.knowledge.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.design.widget.TabLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.jp.knowledge.R;
import com.jp.knowledge.util.L;

import java.lang.reflect.Field;

public class JpTabLayout extends TabLayout {

    public JpTabLayout(Context context) {
        super(context);
        init(context, null);
    }

    public JpTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public JpTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private float mTabMarginStart;
    private float mTabMarginEnd;
    private boolean ignortPaddingInFixed;

    private int mTabPaddingStart;
    private int mTabPaddingEnd;
    private float tabTextSize;
    private LinearLayout tabLayoutView;

    private TextPaint textPaint;

    private void init(Context context, AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JpTabLayout);
        mTabMarginStart = typedArray.getDimension(R.styleable.JpTabLayout_tabMarginStart, 0);
        mTabMarginEnd = typedArray.getDimension(R.styleable.JpTabLayout_tabMarginEnd, 0);
        ignortPaddingInFixed = typedArray.getBoolean(R.styleable.JpTabLayout_ignortPaddingInFixed, false);
        typedArray.recycle();
        try{
            Field paddingStartField = TabLayout.class.getDeclaredField("mTabPaddingStart");
            paddingStartField.setAccessible(true);
            mTabPaddingStart = (int) paddingStartField.get(this);
            paddingStartField.setAccessible(false);

            Field paddingEndField = TabLayout.class.getDeclaredField("mTabPaddingEnd");
            paddingEndField.setAccessible(true);
            mTabPaddingEnd = (int) paddingEndField.get(this);
            paddingEndField.setAccessible(false);

            Field tabTextSizeField = TabLayout.class.getDeclaredField("mTabTextSize");
            tabTextSizeField.setAccessible(true);
            tabTextSize = (float) tabTextSizeField.get(this);
            tabTextSizeField.setAccessible(false);

            Field tabLayoutViewField = TabLayout.class.getDeclaredField("mTabStrip");
            tabLayoutViewField.setAccessible(true);
            tabLayoutView = (LinearLayout) tabLayoutViewField.get(this);
            tabLayoutViewField.setAccessible(false);

            Field tabMinWidth = TabLayout.class.getDeclaredField("mRequestedTabMinWidth");
            tabMinWidth.setAccessible(true);
            tabMinWidth.set(this, 0);
            tabMinWidth.setAccessible(false);

            textPaint = new TextPaint();
            textPaint.setTextSize(tabTextSize);
        } catch (Exception e){
            L.e(e.getMessage());
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        updateTab();
    }

    @Override
    @Deprecated
    public void setTabMode(int mode) {}

    public void updateTab(){
        try {
            boolean isScrollable = false;
            int itemWidth = getWidth()/getTabCount();
            for(int i = 0, j = getTabCount(); i < j; i++){
                CharSequence text = getTabAt(i).getText();
                View tabView = tabLayoutView.getChildAt(i);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();

                float tabWidth = tabView.getWidth();
                float textWidth = textPaint.measureText(text==null ? "" : text.toString());
                float contentWidth = mTabPaddingStart + textWidth + mTabPaddingEnd;
                if(getTabMode() == MODE_SCROLLABLE) {
                    params.width = (int) contentWidth;
                    params.leftMargin = (int) mTabMarginStart;
                    params.rightMargin = (int) mTabMarginEnd;
                } else if(!ignortPaddingInFixed){
                    params.leftMargin = (int) ((tabWidth - contentWidth) / 2);
                    params.rightMargin = params.leftMargin;
                }
                if(mTabMarginStart + contentWidth + mTabMarginEnd > itemWidth)
                    isScrollable = true;
            }
            if(isScrollable && getTabMode() != MODE_SCROLLABLE)
                super.setTabMode(MODE_SCROLLABLE);
            else if(!isScrollable && getTabMode() != MODE_FIXED)
                super.setTabMode(MODE_FIXED);
        } catch (Exception e){
            L.e(e.getMessage());
        }
    }
}
